import ProxyHandler from "../sql/ProxyHandler";
import {BrowserWindow} from "electron";
import {v4 as uuidv4} from 'uuid';
import md5 from "crypto-js/md5";
import SystemMemberMapper from "../dao/SystemMemberMapper";
import SystemMember from "../entity/SystemMember";
import SystemCardMapper from "../dao/SystemCardMapper";
import SystemMemberGradeMapper from "../dao/SystemMemberGradeMapper";
import SystemLoyaltyMapper from "../dao/SystemLoyaltyMapper";

export default class SystemMemberService {
    private mapper: SystemMemberMapper;
    private handler: ProxyHandler<SystemMember>;

    constructor() {
        this.mapper = new SystemMemberMapper()
        this.handler = new ProxyHandler<SystemMember>();
    }

    async page(data){
        const win = BrowserWindow.getFocusedWindow();
        if(win == null){
            throw new Error("应用程序错误")
        }
        let offset = 0, limit = 10
        if(data.current && data.size){
            offset = (data.current - 1) * data.size
            limit = data.size
        }

        let params = {
            tableName: 'system_member',
            offset: offset,
            limit: limit,
            entity: [{
                name: 'name',
                value: data.name,
                operator: 'LIKE'
            },{
                name: 'mobile',
                value: data.mobile,
                operator: 'LIKE'
            }]
        }

        const proxy = new Proxy(this.mapper.page, this.handler)
        const { rows, total } = await proxy(params)

        let result = {
            records: rows ? rows : [],
            total: total ? total : 0,
            current: data.current ? data.current : 1,
            size: limit,
            pages: Math.ceil(total / limit)
        }
        win.webContents.send('main-to-renderer', {
            name: 'member-page-success',
            event: 'event',
            data: result
        })
    }

    async lookup(data){
        const win = BrowserWindow.getFocusedWindow();
        if(win == null){
            throw new Error("应用程序错误")
        }

        const result = await this.mapper.selectList(data)

        win.webContents.send('main-to-renderer', {
            name: 'member-lookup-success',
            event: 'event',
            data: result
        })
    }

    async save(data){
        const win = BrowserWindow.getFocusedWindow();
        if(win == null){
            throw new Error("应用程序错误")
        }
        const entity = new SystemMember();
        entity.id = md5(uuidv4()).toString();
        entity.name = data.name;
        entity.gender = data.gender;
        entity.age = data.age;
        entity.mobile = data.mobile;
        entity.birthday = data.birthday;
        entity.email = data.email;
        entity.wechat = data.wechat;
        entity.qq = data.qq;
        entity.grade = data.grade;
        entity.enabled = data.enabled;

        const res = await this.mapper.selectByMobile(data.mobile)
        if(res != null){
            win.webContents.send('main-to-renderer', {
                name: 'operator-operator-error',
                event: 'event',
                data: '手机号已存在'
            })
            return;
        }

        const proxy = new Proxy(this.mapper.insert, this.handler)
        proxy(entity)
        win.webContents.send('main-to-renderer', {
            name: 'member-operator-success',
            event: 'event',
            data: '操作成功'
        })
    }

    async updateById(data){
        const win = BrowserWindow.getFocusedWindow();
        if(win == null){
            throw new Error("应用程序错误")
        }
        const entity = new SystemMember();
        entity.id = data.id;
        entity.name = data.name;
        entity.gender = data.gender;
        entity.age = data.age;
        entity.mobile = data.mobile;
        entity.birthday = data.birthday;
        entity.email = data.email;
        entity.wechat = data.wechat;
        entity.qq = data.qq;
        entity.grade = data.grade;
        entity.enabled = data.enabled;
        const proxy = new Proxy(this.mapper.updateById, this.handler)
        proxy(entity)

        win.webContents.send('main-to-renderer', {
            name: 'member-operator-success',
            event: 'event',
            data: '操作成功'
        })
    }

    async deleteById(data){
        const win = BrowserWindow.getFocusedWindow();
        if(win == null){
            throw new Error("应用程序错误")
        }
        const entity = new SystemMember();
        entity.id = data;
        const proxy = new Proxy(this.mapper.deleteById, this.handler)
        proxy(entity)
        win.webContents.send('main-to-renderer', {
            name: 'member-operator-success',
            event: 'event',
            data: '操作成功'
        })
    }

    async detail(data){
        const win = BrowserWindow.getFocusedWindow();
        if(win == null){
            throw new Error("应用程序错误")
        }
        const cardMapper = new SystemCardMapper();
        const gradeMapper = new SystemMemberGradeMapper();
        const loyaltyMapper = new SystemLoyaltyMapper();

        const member = await this.mapper.selectById(data);
        const cards = await cardMapper.selectByMemberId(data);
        const grade = await gradeMapper.selectByMemberId(member.grade);
        const loyalty = await loyaltyMapper.selectByMemberId(data);

        const result = {
            member,
            cards: cards ? cards : [],
            grade: grade ? grade : null,
            loyalty: loyalty ? loyalty : null
        }

        win.webContents.send('main-to-renderer', {
            name: 'member-view-success',
            event: 'event',
            data: result
        })
    }

}
